package com.xmut.xmutbackend.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.xmutbackend.entity.Student;
import com.xmut.xmutbackend.entity.StudentStatistics;
import com.xmut.xmutbackend.mapper.StudentMapper;
import com.xmut.xmutbackend.service.IClassService;
import com.xmut.xmutbackend.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private IClassService classService;

    @Override
    public StudentStatistics getStudentStatistics() {
        StudentStatistics statistics = new StudentStatistics();

        // 获取所有学生数据
        List<Student> students = this.list();

        // 打印学生总数
        System.out.println("学生总数: " + students.size());

        // 统计性别分布 - 1为男生，2为女生
        long maleCount = students.stream()
                .filter(s -> s.getGender() != null && s.getGender() == 1)
                .count();

        long femaleCount = students.stream()
                .filter(s -> s.getGender() != null && s.getGender() == 2)
                .count();


        // 打印性别统计结果
        System.out.println("男生数量: " + maleCount);
        System.out.println("女生数量: " + femaleCount);

        statistics.setMaleCount(maleCount);
        statistics.setFemaleCount(femaleCount);

        // 统计年龄段
        Map<String, Long> ageGroupStats = new HashMap<>();
        students.forEach(student -> {
            Integer age = student.getAge();
            String ageGroup;
            if (age == null) {
                ageGroup = "未知";
            } else if (age < 18) {
                ageGroup = "未成年(18岁以下)";
            } else if (age <= 22) {
                ageGroup = "18-22岁";
            } else if (age <= 25) {
                ageGroup = "23-25岁";
            } else {
                ageGroup = "26岁及以上";
            }

            ageGroupStats.put(ageGroup, ageGroupStats.getOrDefault(ageGroup, 0L) + 1);
        });
        statistics.setAgeGroupStats(ageGroupStats);

        // 统计省份分布
        Map<String, Long> provinceStats = students.stream()
                .collect(Collectors.groupingBy(
                        s -> s.getProvince() == null ? "未知" : s.getProvince(),
                        Collectors.counting()
                ));
        statistics.setProvinceStats(provinceStats);

        // 统计入学年份
        Map<String, Long> enrollmentYearStats = students.stream()
                .collect(Collectors.groupingBy(
                        s -> s.getEnrollmentYear() == null ? "未知" : s.getEnrollmentYear(),
                        Collectors.counting()
                ));
        statistics.setEnrollmentYearStats(enrollmentYearStats);

        // 统计班级人数
        Map<String, Long> classStats = new HashMap<>();
        students.forEach(student -> {
            Long classId = student.getClassId();
            if (classId != null) {
                try {
                    String className = classService.getById(classId.intValue()) != null ?
                            classService.getById(classId.intValue()).getName() : "未知班级";
                    classStats.put(className, classStats.getOrDefault(className, 0L) + 1);
                } catch (Exception e) {
                    e.printStackTrace();
                    classStats.put("班级ID-" + classId, classStats.getOrDefault("班级ID-" + classId, 0L) + 1);
                }
            } else {
                classStats.put("未分配班级", classStats.getOrDefault("未分配班级", 0L) + 1);
            }
        });
        statistics.setClassStats(classStats);

        return statistics;
    }
}
